package com.houtai.service.user.job;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.Page;
import com.houtai.base.ResultVo;
import com.houtai.bean.TdCompany;
import com.houtai.bean.TdJob;
import com.houtai.bean.TdUserJob;
import com.houtai.bean.User;
import com.houtai.bean.dto.user.job.JobUserListDto;
import com.houtai.bean.dto.user.job.UserJobInsertDto;
import com.houtai.bean.dto.user.job.UserJobResumeDto;
import com.houtai.bean.vo.resume.TdResumeDetail;
import com.houtai.bean.vo.resume.TdResumeDetailVo;
import com.houtai.bean.vo.user.job.JobUserList;
import com.houtai.bean.vo.user.job.JobUserListVo;
import com.houtai.bean.vo.user.job.UserJobList;
import com.houtai.bean.vo.user.job.UserJobListVo;
import com.houtai.dao.*;
import com.houtai.service.job.JobService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserJobService {
    private static Logger logger = Logger.getLogger(JobService.class);
    @Autowired
    private TdJobDao tdJobDao;
    @Autowired
    private TdCompanyDao tdCompanyDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TdUserJobDao tdUserJobDao;
    @Autowired
    private TdResumeDao tdResumeDao;
    @Autowired
    private TdProjectExperienceDao tdProjectExperienceDao;
    @Autowired
    private TdJobExperienceDao tdJobExperienceDao;
    /**
     * @Purpose:    添加岗位申请记录
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo insertUserJob(UserJobInsertDto dto, String token) {
        String logInfo = this.getClass().getName() + ":insertUserJob:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            TdJob tdJob = tdJobDao.findTdJob(dto.getJob_id());
            if (tdJob != null) {
                User user = (User)redisTemplate.opsForValue().get(token);
                TdResumeDetail tdResume = tdResumeDao.findTdResume(user.getUser_id());
                if (tdResume != null) {
                    if (tdResume.getAttachment_url() != "") {
                        TdUserJob tdUserJob = tdUserJobDao.findTdUserJob(tdJob.getJob_id(), user.getUser_id());
                        if (tdUserJob != null) {
                            retInfo.setMark("1");
                            retInfo.setTip("已申请该岗位");
                        } else {
                            tdUserJob = new TdUserJob();
                            tdUserJob.setCompany_id(tdJob.getCompany_id());
                            tdUserJob.setJob_id(tdJob.getJob_id());
                            tdUserJob.setUser_id(user.getUser_id());
                            tdUserJobDao.insertTdUserJob(tdUserJob);
                            tdJobDao.updateTdJobApplySize(tdJob.getJob_id());
                            retInfo.setMark("0");
                            retInfo.setTip("成功");
                        }
                    } else {
                        retInfo.setMark("1");
                        retInfo.setTip("请先上传附件简历");
                    }
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("请先填写简历");
                }
            } else {
                retInfo.setMark("1");
                retInfo.setTip("岗位不存在");
            }
        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    我的求职记录
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     UserJobListVo
     */
    public UserJobListVo userJobList(Page dto, String token) {
        String logInfo = this.getClass().getName() + ":userJobList:";
        UserJobListVo retInfo = new UserJobListVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);

            String orderBy = "apply_time DESC";
            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), orderBy);
            List<UserJobList> lists = tdUserJobDao.findUserJobList(user.getUser_id());
            PageInfo<UserJobList> list = new PageInfo<>(lists);
            if (list.getTotal() > 0) {

                retInfo.setCurrent_page(dto.getCurrent_page());
                retInfo.setList(list.getList());
                retInfo.setPage_size(list.getPageSize());
                retInfo.setTotal_record(list.getTotal());
                retInfo.setPages(list.getPages());
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("无求职记录");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    岗位投递记录
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     JobUserListVo
     */
    public JobUserListVo jobUserList(JobUserListDto dto, String token) {
        String logInfo = this.getClass().getName() + ":jobUserList:";
        JobUserListVo retInfo = new JobUserListVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            TdCompany tdCompany = tdCompanyDao.findTdCompanyByUserId(user.getUser_id());
            TdJob tdJob = tdJobDao.findTdJob(dto.getJob_id());
            if (tdJob != null) {
                if (tdJob.getCompany_id() == tdCompany.getCompany_id()) {
                    String orderBy = "apply_time DESC";
                    PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), orderBy);
                    List<JobUserList> lists = tdUserJobDao.findJobUserList(dto.getJob_id());
                    PageInfo<JobUserList> list = new PageInfo<>(lists);
                    if (list.getTotal() > 0) {
                        retInfo.setCurrent_page(dto.getCurrent_page());
                        retInfo.setList(list.getList());
                        retInfo.setPage_size(list.getPageSize());
                        retInfo.setTotal_record(list.getTotal());
                        retInfo.setPages(list.getPages());
                        retInfo.setMark("0");
                        retInfo.setTip("查询成功");
                    } else {
                        retInfo.setMark("1");
                        retInfo.setTip("无投递记录");
                    }
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("只能查看自己岗位的投递记录");
                }
            } else {
                retInfo.setMark("1");
                retInfo.setTip("岗位不存在或岗位已关闭");
            }


        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    简历数据详情
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     TdResumeDetailVo
     */
    public TdResumeDetailVo resumeDetail(UserJobResumeDto dto,String token) {
        String logInfo = this.getClass().getName() + ":resumeDetail:";
        TdResumeDetailVo vo = new TdResumeDetailVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            TdCompany tdCompany = tdCompanyDao.findTdCompanyByUserId(user.getUser_id());
            TdUserJob tdUserJob = tdUserJobDao.findTdUserJobById(dto.getJ_u_id());
            if (tdUserJob != null) {
                if (tdUserJob.getCompany_id() == tdCompany.getCompany_id()) {
                    TdResumeDetail tdResumeDetail = tdResumeDao.findTdResume(tdUserJob.getUser_id());
                    if (tdResumeDetail != null) {
                        vo.setJobs(tdJobExperienceDao.findTdJobExperienceList(tdResumeDetail.getResume_id()));
                        vo.setProjects(tdProjectExperienceDao.findTdProjectExperienceList(tdResumeDetail.getResume_id()));
                        vo.setDetail(tdResumeDetail);
                        vo.setMark("0");
                        vo.setTip("查询成功");
                    } else {
                        vo.setMark("1");
                        vo.setTip("未填写简历");
                    }
                } else {
                    vo.setMark("1");
                    vo.setTip("只能查看自己岗位投递记录");
                }

            } else {
                vo.setMark("1");
                vo.setTip("投递记录不存在");
            }



        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            vo.setMark("-1");
            vo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
}
